<template>
	<view>
		<image :src="zoneInfo.image" mode="aspectFill" class="top-banner"></image>
		<view class="page-title-info-box">
			<view class="title-name">{{zoneInfo.name}}</view>
			<view class="desc-box" v-if="zoneInfo.title">
				<view class="desc-content">{{zoneInfo.title}}</view>
				<view class="desc-line"></view>
			</view>
			<view class="class-box">
				<view class="class-item" :class="{'class-item-active': activeIndex == 0}" @click="changeIndex(0, 0)">全部
				</view>
				<view class="class-item" v-for="(item, index) in zoneInfo.childList" :key="item.id"
					:class="{'class-item-active': activeIndex == (index + 1)}" @click="changeIndex(item.id, index+1)">{{item.name}}
				</view>
			</view>
		</view>
		<view class="product-list-box">
			<view class="product-item-box" v-for="item2 in productList" :key="item2.id">
				<!-- <image :src="item2.image" class="product-img" mode="aspectFill"></image> -->
				<view class="product-img">
					<easy-loadimage :image-src="item2.image" :radius="4" :mode="'aspectFill'">
					</easy-loadimage>
				</view>
				<view class="product-info-box">
					<view class="product-title">{{item2.name}}</view>
					<view class="product-tags" v-if="item2.productTags.locationUnderTitle.length">
						<view class="tag-item" v-for="(tag, tagIndex) in item2.productTags.locationUnderTitle" :key="tagIndex">{{tag.tagName}}</view>
						<view class="tag-item">折扣</view>
					</view>
					<view class="product-price-btn">
						<view class="price-num">
							￥<span>{{item2.price}}</span>起
						</view>
						<view class="buy-btn">抢购</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import easyLoadimage from '@/components/easy-loadimage.vue';
	import {
		getFirstZone,
		getZoneProductList
	} from '@/config/api.js'
	export default {
		components: {
			easyLoadimage
		},
		data() {
			return {
				activeIndex: 0,
				zoneInfo: {},
				page: 1,
				productList: [],
				childId: 0,
			};
		},
		onLoad(option) {
			let that = this
			that.getFirstZoneFn(option.id)
		},
		methods: {
			changeIndex(childId, index) {
				let that = this
				that.activeIndex = index
				that.page = 1
				that.productList = []
				that.childId = childId
				that.getZoneProductListFn(childId)
			},
			getFirstZoneFn(id) {
				let that = this
				getFirstZone(id).then(res => {
					console.log('getFirstZone_res', res);
					that.zoneInfo = res.data
					that.page = 1
					that.productList = []
					that.getZoneProductListFn(that.childId) //获取一级活动下全部商品
				})
			},
			getZoneProductListFn(childId) {
				let that = this
				getZoneProductList({
					page: that.page,
					id: childId, // 二级id
					pid: that.zoneInfo.id
				}).then(res=>{
					console.log('getZoneProductListFn_res', res);
					let data = res.data.list
					that.productList.push(...data)
					if (data.length) {
						that.page += 1
					}
				})
			}
		},
		onReachBottom() {
			let that = this
			that.getZoneProductListFn(that.childId)
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f6f6f6;

		view {
			box-sizing: border-box;
		}
	}

	.top-banner {
		width: 100%;
		height: 356rpx;
	}

	.page-title-info-box {
		width: 100%;
		padding: 24rpx 0 28rpx 28rpx;
		background-color: #fff;

		.title-name {
			font-weight: 800;
			font-size: 18px;
			color: #1A1A1A;
		}

		.desc-box {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.desc-content {
				flex-shrink: 0;
				font-size: 24rpx;
				color: #1A1A1A;
				margin-right: 8px;
				margin-top: 6rpx;
			}

			.desc-line {
				flex: 1;
				height: 2rpx;
				background-color: #EEEEEE;
			}
		}

		.class-box {
			width: 100%;
			padding-right: 14px;
			margin-top: 24rpx;
			display: flex;
			flex-wrap: wrap;

			.class-item {
				min-width: 148rpx;
				height: 52rpx;
				border-radius: 3px;
				border: 2rpx solid #9CA3AF;
				padding: 0 18rpx;
				font-size: 14px;
				color: #808080;
				line-height: 52rpx;
				margin-right: 32rpx;
				margin-bottom: 28rpx;
				text-align: center;

				&:nth-of-type(4n) {
					margin-right: 0;
				}

				&:nth-last-child(-n+4) {
					margin-bottom: 0;
				}
			}

			.class-item-active {
				border: 2rpx solid #01c2c3;
				background-color: #01c2c3;
				color: #fff;
				font-weight: 800;
			}
		}
	}

	.product-list-box {
		width: 100%;
		padding: 24rpx 8rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		.product-item-box {
			width: 360rpx;
			margin-bottom: 24rpx;

			&:nth-last-child(-n+2) {
				margin-bottom: 0;
			}

			.product-img {
				width: 100%;
				height: 480rpx;
				border-radius: 8rpx 8rpx 0 0;
			}

			.product-info-box {
				width: 100%;
				padding: 16rpx;
				background-color: #fff;
				height: 206rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.product-title {
					width: 100%;
					height: 64rpx;
					font-weight: 500;
					font-size: 14px;
					color: #1A1A1A;
					line-height: 17px;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.product-tags {
					width: 100%;
					display: flex;
					overflow: hidden;

					.tag-item {
						height: 38rpx;
						border: 1px solid #01C2C3;
						border-radius: 8rpx;
						color: #01C2C3;
						line-height: 38rpx;
						text-align: center;
						padding: 0 14rpx;
						margin-right: 10rpx;
					}
				}

				.product-price-btn {
					width: 100%;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.price-num {
						font-size: 10px;
						color: #000000;

						span {
							font-weight: bold;
							font-size: 16px;
							color: #000000;
						}
					}

					.buy-btn {
						width: 104rpx;
						height: 52rpx;
						background: #01C2C3;
						border-radius: 3px;
						font-weight: 800;
						font-size: 12px;
						color: #FFFFFF;
						text-align: center;
						line-height: 52rpx;
					}
				}
			}
		}
	}
</style>